package com.check.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.check.dao.MarkTableDao;
import com.check.pojo.MarkTable;
import com.check.pojo.MarkTableMeasure;
import com.check.pojo.MarkTableRule;
import com.check.pojo.PageObject;
import com.check.service.MarkTableService;

@Service("markTableService")
public class MarkTableServiceImpl implements MarkTableService{

	@Autowired
	@Qualifier("markTableDao")
	private MarkTableDao markTableDao;
	
	public List<MarkTable> findAllTable(PageObject pageObject) {
		Map<String , Integer> map=new HashMap<String, Integer>();
		map.put("startNum", pageObject.getStartNum());
		map.put("endNum", pageObject.getEndNum());
		return markTableDao.findAllTable(map);
	}

	public int findTableCount() {
		return markTableDao.findTableCount();
	}

	public MarkTable findTableById(String tbId) {
		MarkTable markTable=null;
		try {
			markTable=markTableDao.findTableById(tbId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return markTable;
	}

	public boolean insertMarkTable(MarkTable markTable) {
		boolean flag=false;
		try {
			markTableDao.insertMarkTable(markTable);
			flag=true;
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public boolean deleteMarkTable(String tbId) {
		boolean flag=false;
		try {
			markTableDao.deleteMarkTable(tbId);
			markTableDao.deleteAllMarkMeasureByTbId(tbId);
			flag=true;
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public boolean updateMarkTable(MarkTable markTable) {
		boolean flag=false;
		try {
			markTableDao.updateMarkTable(markTable);
			flag=true;
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public List<MarkTableMeasure> findAllMeasure(PageObject pageObject,String tbId) {
		Map<Object,Object> map=new HashMap<Object, Object>();
		map.put("startNum", pageObject.getStartNum());
		map.put("endNum", pageObject.getEndNum());
		map.put("tbId", tbId);
		return markTableDao.findAllMeasure(map);
	}

	public int findMeasureCount(String tbId) {
		return markTableDao.findMeasureCount(tbId);
	}

	public boolean insertMarkTableMeasure(MarkTableMeasure markTableMeasure) {
		boolean flag=false;
		try {
			markTableDao.insertMarkTableMeasure(markTableMeasure);
			flag=true;
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public boolean insertMarkTable(MarkTable markTable,List<MarkTableMeasure> measureList) {
		boolean flag=false;
		try {
			markTableDao.insertMarkTable(markTable);
			for (MarkTableMeasure markTableMeasure : measureList) {
				markTableDao.insertMarkTableMeasure(markTableMeasure);
			}
			flag=true;
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public List<MarkTableMeasure> findAllMeasure(String tbId) {
		return markTableDao.findAllMeasure(tbId);
	}

	public boolean updateMarkTable(MarkTable markTable,List<MarkTableMeasure> markTableMeasureList) {
		boolean flag=false;
		try {
			markTableDao.updateMarkTable(markTable);
			markTableDao.deleteAllMarkMeasureByTbId(markTable.getTbId());
			for (MarkTableMeasure markTableMeasure : markTableMeasureList) {
				markTableDao.insertMarkTableMeasure(markTableMeasure);
			}
			flag=true;
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public List<MarkTable> findAllActiveTable() {
		return markTableDao.findAllActiveTable();
	}

	public List<MarkTableRule> findAllMarkTableRule(String tbId) {
		return markTableDao.findAllMarkTableRule(tbId);
	}

	public boolean insertMarkTableRule(String tbId,List<MarkTableRule> markTableRuleList) {
		boolean flag=false;
		try {
			markTableDao.deleteMarkTableRule(tbId);
			for (MarkTableRule markTableRule : markTableRuleList) {
				markTableDao.insertMarkTableRule(markTableRule);
			}
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public List<MarkTableRule> findAllMarkTableDetailRule(String tbId) {
		return markTableDao.findAllMarkTableDetailRule(tbId);
	}

	public boolean deleteMarkTableRule(String tbId) {
		boolean flag=false;
		try {
			markTableDao.deleteMarkTableRule(tbId);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	

}
